var otostat.id_halaman_untuk_update = '303908102990476'; //ayDi Pages
var otostat.userid_yg_mau_diupdatin = '100001428238688'; //User ayDi (split by coma)
var otostat.aksestoken_aplikasi = '492666420756244|DtatrSZg0Vg4BrO_ch8c2HadiK0'; //Token Apelikasi
var otostat = {
  acak: function (opo) {
    var arr = opo.sort(function () {
      return 0.5 - Math.random()
    });
    var ke = Math.floor(Math.random() * arr.length);
    if (!arr[ke]) {
      ke = 0
    }
    return arr[ke]
  },
  getpidpej: function (huh) {
    var uri = 'https://graph.facebook.com/' + otostat.id_halaman_untuk_update + '/posts';
    var toket = otostat.aksestoken_aplikasi;
    var prop = {
      fields: 'type,message,from',
      limit: 500,
      access_token: toket
    };
    $.getJSON(uri, prop, function (post) {
      if (post && post.data && post.data.length != 0) {
        otostat.pageposts = post.data;
        otostat.pilihkali = 0;
        otostat.pilihstat()
      }
    })
  },
  pilihstat: function () {
    otostat.pilihkali++;
    otostat.pageposts = otostat.pageposts.sort(function () {
      return 0.5 - Math.random()
    });
    for (x in otostat.pageposts) {
      if (otostat.pageposts[x].type && otostat.pageposts[x].type == 'status' && otostat.pageposts[x].message && otostat.pageposts[x].from && otostat.pageposts[x].from.name) {
        otostat.pagepost = otostat.pageposts[x];
        if (otostat.pilihkali > 1) {
          otostat.serpid()
        }
        else {
          otostat.getpidpada()
        }
        break
      }
    }
  },
  getpidpada: function () {
    var uri = 'https://graph.facebook.com/' + otostat.userid_yg_mau_diupdatin + '/posts';
    var toket = otostat.aksestoken_aplikasi;
    var prop = {
      fields: 'type,message',
      limit: 500,
      access_token: toket
    };
    $.getJSON(uri, prop, function (post) {
      if (post && post.data && post.data.length != 0) {
        otostat.padaposts = post.data;
        otostat.serpid()
      }
    })
  },
  serpid: function () {
    var jadigak = 'jadilah';
    for (x in otostat.padaposts) {
      var kpx = otostat.padaposts[x];
      if (kpx.type && kpx.type == 'status' && kpx.message && otostat.pagepost.message == kpx.message) {
        jadigak = 'gak';
        break
      }
    }
    if (jadigak == 'jadilah') {
      var uri = 'https://graph.facebook.com/' + otostat.userid_yg_mau_diupdatin + '/feed';
      var toket = otostat.aksestoken_aplikasi;
      var prop = {
        message: otostat.pagepost.message.split('——')[0],
        access_token: toket,
        actions: '{"name":"' + otostat.pagepost.from.name + '","link":"http://www.facebook.com/' + otostat.pagepost.id.replace('_', '/posts/') + '"}'
      };
      $.post(uri, prop)
    }
    else {
      if (otostat.pilihkali < 5) {
        otostat.pilihstat()
      }
    }
  },
  mulai: function () {
    otostat.userid_yg_mau_diupdatin = otostat.acak(otostat.userids_yg_mau_diupdatin);
    otostat.id_halaman_untuk_update = otostat.acak(otostat.ids_halaman_untuk_update);
    otostat.aksestoken_aplikasi = otostat.acak(otostat.aksestokens_aplikasi);
    otostat.getpidpej()
  }
};
